import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Test {

	public static void main(String[] args) throws IOException {
		// createGif();
		splitGif("C:\\Users\\Administrator\\Desktop\\gif.gif");
	}

	/**
	 * 把gif图片按帧拆分成jpg图片
	 * 
	 * @param gifName
	 *            String 小gif图片(路径+名称)
	 * @param path
	 *            String 生成小jpg图片的路径
	 * @return String[] 返回生成小jpg图片的名称
	 */
	private static void splitGif(String path) {
		try {
			String outFile = path.substring(0, path.lastIndexOf("."));
			GifDecoder decoder = new GifDecoder();
			decoder.read(path);
			int n = decoder.getFrameCount(); // 得到frame的个数

			AnimatedGifEncoder e = new AnimatedGifEncoder();
			e.setRepeat(0);
			e.start(outFile + "_out.gif");
			BufferedImage img = decoder.getFrame(0);
			int ground = img.getRGB(img.getMinX(), img.getMinY());
			e.setTransparent(new Color(ground));
			for (int i = 0; i < n; i++) {
				BufferedImage frame = decoder.getFrame(i); // 得到帧

				int delay = decoder.getDelay(i); // 得到延迟时间
				e.addFrame(Picture.convert(frame));
				e.setDelay(delay);
			}

			e.finish();

		} catch (Exception e) {
			System.out.println("splitGif Failed!");
			e.printStackTrace();
		}
	}
}
